package com.etc.utils;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 *
 * @author 臧浩鹏
 */
public class DateTimeUtil {

	// joda-time

	// str->Date
	// Date->str
	/**
	 *
	 * @Description: 格式化通配
	 *
	 * @auther: 臧浩鹏
	 * @date: 10:03 2018/7/25
	 * @param:
	 * @return:
	 *
	 */
	public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static String randomDate() {
		Random i = new Random();
		int i1 = i.nextInt(10);
		List<String> dates = new ArrayList();
		dates.add("2018-08-02 09:19:15");
		dates.add("2018-08-06 09:25:33");
		dates.add("2018-08-07 09:56:56");
		dates.add("2018-08-04 10:00:36");
		dates.add("2018-08-06 15:19:10");
		dates.add("2018-08-06 15:19:19");
		dates.add("2018-08-02 09:29:15");
		dates.add("2018-08-06 09:22:33");
		dates.add("2018-08-07 09:54:56");
		dates.add("2018-08-04 12:00:36");
		dates.add("2018-08-06 14:29:10");
		dates.add("2018-08-06 12:29:19");
		return dates.get(i1);
	}

	public static Date strToDate(String dateTimeStr, String formatStr) {
		DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
		DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
		return dateTime.toDate();
	}

	public static String dateToStr(Date date, String formatStr) {
		if (date == null) {
			return StringUtils.EMPTY;
		}
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(formatStr);
	}

	public static Date strToDate(String dateTimeStr) {
		DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
		DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
		return dateTime.toDate();
	}

	public static String dateToStr(Date date) {
		if (date == null) {
			return StringUtils.EMPTY;
		}
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(STANDARD_FORMAT);
	}

	public static void main(String[] args) {
		System.out.println(DateTimeUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
		System.out.println(DateTimeUtil.strToDate("2010-01-01 11:11:11", "yyyy-MM-dd HH:mm:ss"));

	}

}
